package com.sc.nft.dao;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolCommunityServiceRankConf;
import com.sc.nft.enums.EcologyPoolCommunityServiceRankApprovalStatusEnum;
import com.sc.nft.enums.EcologyPoolCommunityServiceRankTypeEnum;
import com.sc.nft.mapper.EcologyPoolCommunityServiceRankConfMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author PC
 * @description 针对表【sc_ecology_pool_community_service_rank_conf(生态池-服务中心/社区网点榜单配置)】的数据库操作Dao
 * @createDate 2024-07-22 13:48:10
 */
@Slf4j
@Repository
public class EcologyPoolCommunityServiceRankConfDao extends ServiceImpl<EcologyPoolCommunityServiceRankConfMapper, EcologyPoolCommunityServiceRankConf> {

    /**
     * 分页查询
     *
     * @param page
     * @param rankName
     * @return
     */
    public Page<EcologyPoolCommunityServiceRankConf> page(Page<EcologyPoolCommunityServiceRankConf> page, String rankName, EcologyPoolCommunityServiceRankApprovalStatusEnum approvalStatus, Long daoId, Long neDaoId) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRankConf> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(rankName), EcologyPoolCommunityServiceRankConf::getRankName, rankName);
        queryWrapper.eq(Objects.nonNull(approvalStatus), EcologyPoolCommunityServiceRankConf::getApprovalStatus, approvalStatus);
        queryWrapper.eq(Objects.nonNull(daoId), EcologyPoolCommunityServiceRankConf::getDaoId, daoId);
        queryWrapper.ne(Objects.nonNull(neDaoId), EcologyPoolCommunityServiceRankConf::getDaoId, neDaoId);
        queryWrapper.orderByDesc(EcologyPoolCommunityServiceRankConf::getCreateTime);
        return page(page, queryWrapper);
    }

    /**
     * 查询最大结束时间
     *
     * @return
     */
    public EcologyPoolCommunityServiceRankConf getMaxEndTime(EcologyPoolCommunityServiceRankTypeEnum rankType, Long daoId) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRankConf> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(EcologyPoolCommunityServiceRankConf::getRankType, rankType);
        queryWrapper.eq(EcologyPoolCommunityServiceRankConf::getDaoId, daoId);
        queryWrapper.orderByDesc(EcologyPoolCommunityServiceRankConf::getEndTime);
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 获取最新开启的配置
     *
     * @return
     */
    public EcologyPoolCommunityServiceRankConf getLastActive(EcologyPoolCommunityServiceRankTypeEnum rankType, Long daoId) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRankConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getStatus, Boolean.TRUE);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getApprovalStatus, EcologyPoolCommunityServiceRankApprovalStatusEnum.APPROVAL_SUCCESS);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getRankType, rankType);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getDaoId, daoId);
        wrapper.orderByDesc(EcologyPoolCommunityServiceRankConf::getStartTime);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 历史榜单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<EcologyPoolCommunityServiceRankConf> historyRankPage(int pageNo, int pageSize, EcologyPoolCommunityServiceRankTypeEnum rankType, Long daoId) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRankConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getRankType, rankType);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getDaoId, daoId);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getApprovalStatus, EcologyPoolCommunityServiceRankApprovalStatusEnum.APPROVAL_SUCCESS);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getStatus, Boolean.TRUE);
        wrapper.lt(EcologyPoolCommunityServiceRankConf::getEndTime, new Date());
        wrapper.orderByDesc(EcologyPoolCommunityServiceRankConf::getStartTime);
        return page(new Page<>(pageNo, pageSize), wrapper);
    }

    public List<EcologyPoolCommunityServiceRankConf> getActiveRankings() {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRankConf> wrapper = Wrappers.lambdaQuery();
        // 时间在开始时间和节数时间之内的数据
        wrapper.le(EcologyPoolCommunityServiceRankConf::getStartTime, DateUtil.date());
        wrapper.ge(EcologyPoolCommunityServiceRankConf::getEndTime, DateUtil.date());
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getStatus, Boolean.TRUE);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getApprovalStatus, EcologyPoolCommunityServiceRankApprovalStatusEnum.APPROVAL_SUCCESS);
        return list(wrapper);
    }

    public List<EcologyPoolCommunityServiceRankConf> getWaitAutoSettleRankings() {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRankConf> wrapper = Wrappers.lambdaQuery();
        // 排行榜已结束
        wrapper.lt(EcologyPoolCommunityServiceRankConf::getEndTime, DateUtil.date());
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getStatus, Boolean.TRUE);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getApprovalStatus, EcologyPoolCommunityServiceRankApprovalStatusEnum.APPROVAL_SUCCESS);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getAutoSettle, Boolean.TRUE);
        wrapper.eq(EcologyPoolCommunityServiceRankConf::getSettled, Boolean.FALSE);
        return list(wrapper);
    }

}
